package com.yycx.module.bus.provider.handler.haiying.order.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 已开票订单列表
 *
 * @author zyf
 */
@Component
public class HyInvoiceOrderTab2ListHandler implements PageInterceptor<HyOrder> {


    @Autowired
    private TaskService taskService;

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private BusHyorderSendinfoService hyorderSendinfoService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        cq.eq(HyOrder.class, "companyId");
        String orderNo = cq.getParams("orderNo");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        cq.eq("orderState", 2);
        cq.eq("invoiceState", 2);
        cq.eq(HyOrder.class, "companyId");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Long companyId = entityMap.get("companyId");
            String orderId = entityMap.get("orderId");
            //开票信息
            BusHyorderSendinfo sendInfo = hyorderSendinfoService.findByOrderId(orderId);
            if (FlymeUtils.isNotEmpty(sendInfo)) {
                entityMap.put("invoiceNo", sendInfo.getInvoiceNo());
            }
            if (FlymeUtils.isNotEmpty(companyId)) {
                SysCompany company = companyService.getById(companyId);
                entityMap.put("companyName", company.getCompanyName());
            }
        }
        return result;
    }

}
